package com.cxdr.awsips.common.thread;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.log4j.Logger;

import com.cxdr.awsips.common.bean.Thread_log;
import com.cxdr.awsips.common.logger.LogFactory;

public class ThreadRecorder extends BaseProcessor{
	private static Logger logger = LogFactory
			.makeNewLogger(ThreadRecorder.class);
	public static final String TASK_NAME = "ThreadMirror";
	public static final String THREAD_NAME = "ThreadPool-thread";
	private static Map<Long,BaseProcessor> pool = new HashMap<Long,BaseProcessor>();
	private static boolean started = false;
	private static Timer recorder = new Timer();
	private static ThreadRecorder recoder  ;
	private ThreadRecorder(){
		setTask_name(TASK_NAME);
		setThread_name(THREAD_NAME);
	}
	public static ThreadRecorder getInstance(){
		if(recoder == null){
			recoder = new ThreadRecorder();
		}
		return recoder;
	}
	public void startWork(){
		if (!isStarted()) {
			recorder.schedule(new TimerTask() {
				@Override
				public void run() {
					// TODO 向数据库写入状态
					for(Entry<Long, BaseProcessor> processor:pool.entrySet()) {
						Thread_log log = processor.getValue().getLog();
						//写入数据库
					}
					
				}
			}, 0,15000);
		}
	}
	
	public void stopWork() {
		if (!isStarted()){
			logger.info("ThreadPool:  stop work");
			recorder.cancel();
			setStarted(false);
		}
	}
	
	public boolean isStarted() {
		return started;
	}

	public void setStarted(boolean started) {
		this.started = started;
	}
	
	public static void reg(long id,BaseProcessor thread){
		pool.put(id	, thread);
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		
	}
	
}
